package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class ListPage extends Utils {
    //
    public static String url = "http://124.223.114.169:8081/list.html";
    public ListPage(){
        super(url);
    }

    /**
     * 列表页成功加载
     * 必要元素存在
     */
    public void listRight(){
        System.out.println("listRight-start");

        driver.findElement(By.cssSelector("#body > div.container > h3"));
        System.out.println("listRight-finished");

    }

    public void listen() {
        driver.findElement(By.cssSelector("#info > tr:nth-child(1) > td:nth-child(4) > button")).click();
    }

    public void select() {
        System.out.println("select-start");

        WebElement ele =  driver.findElement(By.cssSelector("#exampleInputName2"));
        ele.sendKeys("山");
        ele.click();
        //页面显示查询结果（需要设置等待，防止找不到元素--不过，Utils中，已经对查询元素设置了等待，全局有效）
        driver.findElement(By.cssSelector("#info > tr > td:nth-child(2)"));
        //重新进入列表页，防止已经上传过的情况而导致页面跳转，影响后续该页面的测试用例执行
        driver.get(url);
        System.out.println("select-finish");

    }

    public void loveAdd() throws InterruptedException {
        System.out.println("loveAdd-start");

        Thread.sleep(2000);
        driver.findElement(By.cssSelector("#info > tr:nth-child(1) > td:nth-child(6) > button")).click();
        Thread.sleep(1000);
        Alert alert1 = driver.switchTo().alert();
        String text1 = alert1.getText();
        alert1.accept();//有个疑问，alert框中的信息，能获取嘛？

        driver.findElement(By.cssSelector("#info > tr:nth-child(1) > td:nth-child(6) > button")).click();
        Thread.sleep(1000);
        Alert alert2 = driver.switchTo().alert();
        String text2 = alert2.getText();
        alert2.accept();//注意，这里不要忘记处理弹窗
        if(text1.equals("收藏失败！")) {
            assert text2.equals("收藏成功！");
        }else {
            assert text2.equals("收藏失败");
        }

        System.out.println("loveAdd-finish");


    }

    public void upload() throws InterruptedException {
        System.out.println("upload-start");
        //这里的upload是不是只要上传文件名字就可以呀？？？
        driver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(2)")).click();
        //跳转至新的页面，需要等待
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)")).sendKeys("D:\\MyMusic\\月光.mp3");//注意这里，直接复制过来的路径可能含有引号，记得删掉
        driver.findElement(By.cssSelector("body > form > input[type=submit]:nth-child(3)")).click();
        //重新进入列表页，防止已经上传过的情况而导致页面跳转，影响后续该页面的测试用例执行
        driver.get(url);
        System.out.println("upload-finish");
        driver.get(url);


    }

    public void deleteSingle() throws InterruptedException {
        System.out.println("deleteSingle-start");

        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#info > tr:nth-child(1) > td:nth-child(5) > button")).click();
        Thread.sleep(1000);//等一下弹窗
        Alert alert = driver.switchTo().alert();
        alert.accept();
        System.out.println("deleteSingle-finish");


    }

    public void deleteGroup() throws InterruptedException {
        System.out.println("deleteGroup-start");
        Thread.sleep(3000);

        driver.findElement(By.cssSelector("#info > tr:nth-child(1) > th > input")).click();
        driver.findElement(By.cssSelector("#info > tr:nth-child(2) > th > input")).click();
        driver.findElement(By.cssSelector("#delete")).click();
        Thread.sleep(1000);//等一下弹窗
        Alert alert = driver.switchTo().alert();
        alert.accept();
        System.out.println("deleteGroup-finish");

    }
}
